#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: note
    Description:
        Author: lhw
        Date: 2020/9/23
-----------------------------------
"""
from rest_framework.views import APIView
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from Baby import models
from Baby.serializers import note
from Common.logger import app_log
from Common.response import resp


class NoteView(APIView):
    # authentication_classes = [JwtAuthorizationAuthentication, ]

    def get(self, request, *args, **kwargs):
        try:
            baby_id = request.query_params.get("id", "")
            hospital = request.query_params.get("hospital", "")
            note_query = models.Note.objects.filter(babies=baby_id, hospital=hospital).all()
            note_data = note.NoteModelSerializer(note_query, many=True).data
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": note_data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def post(self, request, *args, **kwargs):
        try:
            request_data = request.data
            note_ser = note.NoteModelSerializer(data=request_data)
            note_ser.is_valid(raise_exception=True)
            note_obj = note_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": note.NoteModelSerializer(note_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def put(self, request, *args, **kwargs):
        try:
            request_data = request.data
            note_id = request_data["id"]
            old_note_obj = models.Note.objects.filter(id=note_id).first()
            note_ser = note.NoteModelSerializer(instance=old_note_obj,
                                                data=request_data, partial=True)
            note_ser.is_valid(raise_exception=True)
            note_obj = note_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": note.NoteModelSerializer(note_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def delete(self, request, *args, **kwargs):
        try:
            note_id = request.GET.get("id")
            note_date = models.Note.objects.filter(id=note_id).first()
            if note_date:
                models.Note.objects.filter(id=note_id).delete()
                return Response({
                    "error_code": 0,
                    "message": "Success"
                })
            else:
                return resp(2005, {})
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

